import java.sql.*;
import java.util.*;

public class manager {
    public void welcome() {
        System.out.println("1.查看 2.添加 3.修改 4.删除 5.查看营业额");
        Scanner in = new Scanner(System.in);
        int choice = in.nextInt();
        if (choice == 1) {
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
            } catch (ClassNotFoundException q) {
                q.printStackTrace();
            }
            try {
                Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/rentcar", "root", "123456");
                Statement stm = conn.createStatement();
                ResultSet r = stm.executeQuery("select * from car ");
                System.out.println("车牌号品牌\t品牌\t\t\t类型\t\t\t日租金\t\t座位或载重量");
                while (r.next()) {
                    System.out.printf("%-12s", r.getString(1));
                    System.out.printf("%-12s", r.getString(2));
                    System.out.printf("%-12s", r.getString(3));
                    System.out.printf("%-12s", r.getInt(4));
                    System.out.printf("%-18s", r.getString(5));
                    System.out.println();
                    //管理员操作入口
                }
                r.close();
                stm.close();
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        } else if (choice == 2) {
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
            } catch (ClassNotFoundException q) {
                q.printStackTrace();
            }
            try {
                Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/rentcar", "root", "123456");
                Statement stm = conn.createStatement();

                Scanner sc= new Scanner(System.in);
                System.out.println("请输入品牌");
                String id = sc.next();
                System.out.println("请输入类型");
                String type = sc.next();
                System.out.println("请输入日租金价格");
                int perRent = sc.nextInt();
                System.out.println("请输入座位或载重量");
                int seatcount_or_ton = sc.nextInt();

                PreparedStatement pstm = conn.prepareStatement("INSERT into car values (?,?,?,?,?)");

                pstm.setString(1, "******");
                pstm.setString(2, id);
                pstm.setString(3,type);
                pstm.setInt(4,perRent);
                pstm.setInt(5,seatcount_or_ton);

                pstm.executeUpdate();

                pstm.close();
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        } else if (choice == 3) {
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
            } catch (ClassNotFoundException q) {
                q.printStackTrace();
            }
                try {
                    Class.forName("com.mysql.cj.jdbc.Driver");
                } catch (ClassNotFoundException q) {
                    q.printStackTrace();
                }
                try {
                    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/rentcar", "root", "123456");

                    Scanner sc= new Scanner(System.in);
                    System.out.println("请输入品牌");
                    String id = sc.next();
                    System.out.println("请输入类型");
                    String t = sc.next();
                    System.out.println("请输入修改后的日租金价格");
                    int rent = sc.nextInt();
                    System.out.println("请输入修改后的座位或载重量");
                    int count_or_ton = sc.nextInt();
                    PreparedStatement pstm = conn.prepareStatement("update car set perRent = ?,seatCount_or_Ton=? where brand = ? and type = ?");
                    pstm.setInt(1,rent);
                    pstm.setInt(2,count_or_ton);
                    pstm.setObject(3, id);
                    pstm.setObject(4, t);
                    pstm.executeUpdate();
                    pstm.close();
                    conn.close();
                    System.out.println("车辆信息修改成功！");
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        else if(choice == 4)
        {
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
            } catch (ClassNotFoundException q) {
                q.printStackTrace();
            }
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
            } catch (ClassNotFoundException q) {
                q.printStackTrace();
            }
            try {
                Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/rentcar", "root", "123456");

                Scanner sc = new Scanner(System.in);
                System.out.println("请输入品牌");
                String id = sc.next();
                System.out.println("请输入类型");
                String t = sc.next();

                PreparedStatement pstm = conn.prepareStatement("DELETE from car where  brand = ? and type = ?");

                pstm.setObject(1, id);
                pstm.setObject(2, t);
                pstm.executeUpdate();

                pstm.close();
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        else
        {
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
            } catch (ClassNotFoundException q) {
                q.printStackTrace();
            }
            try {
                Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/rentcar", "root", "123456");
                Statement stm = conn.createStatement();
                ResultSet rs = stm.executeQuery("select * from money ");
                while (rs.next()) {
                    System.out.printf("当前营业额：%-12f RMB", rs.getFloat(1));
                }
                rs.close();
                stm.close();
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
